-- CravenSkeleton
-- Created by chengb Feb/3/2016
-- 胆小的骷髅格子

-- 初始化
function initCravenSkeleton(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 点击"给他指骨！"的处理
    local function okFunc()
        -- 播放骷髅播放逃跑光效，并浮现文字提示，随后该建筑替换为道具【半块符石】
        playSkeletonFleeEffect(self);

        self.item:setVisible(false);

        alert(getLocStr("craven_skeleton_leave"));
        
        local pos = self.gridData:getPos();

        local function transform()
            DungeonActionM.go("element_transform", pos);
        end

        performWithDelay(self, transform, 0.5);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开建筑查看界面2
            self:openBuildingView2(okFunc);
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end

